/**
 * Title:        流程活动版本检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.general.Util;



public class WorkflowVersionCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowVersionCheck  start");
        List resultList = new ArrayList();
        recordSet.executeSql("select 1 as type,id,version,workflowname from workflow_base where isvalid <> '3' and id <> activeversionid and activeversionid is not null "
                +" union all select 2 as type,id,version, workflowname from workflow_base where isvalid = '3' and (id = activeversionid ) "
                +" union all select 3 as type,id,version, workflowname from workflow_base where isvalid = '3' and (id = activeversionid or activeversionid is null) ");
        int cnt = 0;
        while(recordSet.next()){
            int type = recordSet.getInt("type");
            Map resultMap = new HashMap<String,String>();
            int version = recordSet.getInt("version");
            if(type == 1){
                resultMap.put("showMessage",formatMessage(recordSet.getString("workflowname"), "存在多个活动版本。", "id=" + recordSet.getString("id") + " 版本" + version));
                resultMap.put("buttonname","修复");
            }else{
                resultMap.put("showMessage",formatMessage(recordSet.getString("workflowname"), "没有活动版本。", "id=" + recordSet.getString("id") + " 版本" + version));
                if(type == 2){
                    resultMap.put("buttonname","修复");
                }else{
                    resultMap.put("buttonname","设为活动版本");
                }
            }
            resultMap.put("workflowid", recordSet.getInt("id"));
            resultMap.put("type", recordSet.getInt("type"));
            resultList.add(resultMap);
            /*
            if(type == 1){
                resultList.add("【" + recordSet.getString("workflowname") + "】" + "存在多个活动版本。");
            }else{
                resultList.add("【" + recordSet.getString("workflowname") + "】" + "没有活动版本。");
            }
            */
            cnt++;
        }
        String msg = "";
        String result = "";
        if(cnt == 0){
            result = resultOk;
            msg="所有流程版本都正确。";
        }else{
            result = resultNeedFix;
            msg="有流程需要修复。";
        }
        (new BaseBean()).writeLog("WorkflowVersionCheck  end");
        return formatReturnJsonCheck(result, resultList, this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {

        (new BaseBean()).writeLog("WorkflowVersionCheck  start");
        setRecordSet();
        if(dataMap.get("workflowid") != null && !dataMap.get("workflowid").equals("")){
            //根据参数获取列表
            String workflowids = (String)dataMap.get("workflowid");
            String types =  (String)dataMap.get("type");
            String divids =  (String)dataMap.get("divid");
            List<String> workflowidList = Util.splitString2List(workflowids, ",");
            List<String> typeList = Util.splitString2List(types, ",");
            List<String> dividList = Util.splitString2List(divids, ",");
            List resultList = new ArrayList();
            String result = resultOk;
            for(int i = 0; i < workflowidList.size(); i++){
                String workflowid = workflowidList.get(i);
                String type = typeList.get(i);
                String divid = dividList.get(i);
                if("1".equals(type)){
                    recordSet.executeUpdate("update workflow_base set isvalid = 3 where id =" + workflowid);
                }else{
                    recordSet.executeUpdate("update workflow_base set isvalid = 1 where id =" + workflowid);
                }
                recordSet.executeSql("select 1 from (select 1 as type,id,workflowname from workflow_base where isvalid <> '3' and id <> activeversionid and activeversionid is not null "
                        +" union all select 2 as type,id, workflowname from workflow_base where isvalid = '3' and (id = activeversionid ) "
                        +" union all select 3 as type,id, workflowname from workflow_base where isvalid = '3' and (id = activeversionid or activeversionid is null) "
                        +" ) aaa where id = " + workflowid);
                int cnt = 0;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("divid",divid);
                if(recordSet.next()){
                    result = resultNeedFix;
                    resultMap.put("result",resultNeedFix);
                    resultMap.put("fixMessage","修复失败");
                }else{
                    resultMap.put("result",resultOk);
                    resultMap.put("fixMessage","修复成功");
                }
                resultList.add(resultMap);
            }
            return formatReturnJsonFix(result, resultList,this);
        }else{

            List resultList = new ArrayList();
            recordSet.executeUpdate("update workflow_base set isvalid = 3 where isvalid <> '3' and id <> activeversionid and activeversionid is not null ");
            recordSet.executeUpdate("update workflow_base set isvalid = 1 where isvalid = '3' and id = activeversionid ");
            recordSet.executeUpdate("update workflow_base set isvalid = 1 where isvalid = '3' and (id = activeversionid or activeversionid is null) ");

            String result = "";
            String msg = "";

            //重新加载DIV
            needReload = "1";
            return checkData(new HashMap());
        }
    }

    @Override
    public String getClassName() {
        return "流程活动版本检测";
    }

    @Override
    public String getClassDesc() {
        return "检测流程活动版本设置是否正常";
    }

}



